package woniuxy.commons;





import woniuxy.util.SqlSessionUtil;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * ClassName:JDKProxy
 * Package:util
 *
 * @date:2021/11/2 4:13 下午
 * @author: Tiangle
 */
public class JDKProxy<T> implements InvocationHandler {
    private T t;

    public T getProxy(T real){
        t = real;
        return (T) Proxy.newProxyInstance(real.getClass().getClassLoader(),
                real.getClass().getInterfaces(),this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object returnValue = null;
        try {
            returnValue = method.invoke(t,args);
            SqlSessionUtil.getSqs().commit();
        }catch (Exception e){
            e.printStackTrace();
            SqlSessionUtil.getSqs().rollback();
        }


        return returnValue;
    }
}
